package expressions;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import values.Value;
import base.Environment;
import base.InputParser;

public class Input implements Expression {

	private boolean alreadyEntered;
	private Value value;

	/**
	 * Creates an expression that that will be equal to what the user inputs
	 * when it is evaluated.
	 * 
	 */

	public Input() {
		this.alreadyEntered = false;
	}

	/**
	 * Return the value that the user entered, by parsing the String entered
	 * with Parser.parse() method.
	 * 
	 */

	public Value eval(Environment eval) {

		if (!alreadyEntered) {
			BufferedReader br = new BufferedReader(new InputStreamReader(
					System.in));

			String s = null;

			try {
				s = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
				System.exit(1);
			}

			this.alreadyEntered = true;
			this.value = InputParser.parse(s);
		}

		return value;
	}

}
